package com.lagou.encoder;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class RpcDecoder<T> extends MessageToMessageDecoder<ByteBuf> {
    private Class<T> clazz;
    private Serializer serializer;

    public RpcDecoder(Class<T> clazz, Serializer serializer) {
        this.clazz = clazz;
        this.serializer = serializer;
    }

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf o, List<Object> list) throws Exception {
        Object request = serializer.deserialize(clazz, o.toString(StandardCharsets.UTF_8).getBytes());
        list.add(request);
    }
}
